package accs.model.uploaded;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import accs.model.AccountChecker;
import accs.model.Context;
import accs.model.ExtendedHttpClient;
import accs.model.utils.HeaderUtils;

public class UploadedChecker implements AccountChecker {

	
	private static final Pattern EMAIL_PATTERN=Pattern.compile("<th id=\"chMail\" title=\"([0-9A-za-z.@]+)\">");
	private static final Pattern TIME_PATTERN=Pattern.compile("");
	
	public UploadedAccount login(String user, String password, Context ctx) throws Exception{
		
		DefaultHttpClient httpclient=new ExtendedHttpClient(ctx);
		
		List<NameValuePair> list=new ArrayList<NameValuePair>();
//		for(NameValuePair bla : list) {
//			System.out.println(bla.getName()+" -> "+bla.getValue());
//		}
		
		HttpPost post=new HttpPost("http://uploaded.net/io/login");
		post.setHeader(HeaderUtils.getUserAgent());
		post.setHeader(HeaderUtils.getReferer("http://uploaded.net/login"));
				
		
		list.add(new BasicNameValuePair("id", user));
		list.add(new BasicNameValuePair("pw", password));
		post.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));

		HttpResponse resp2=httpclient.execute(post);
//		String location=resp2.getFirstHeader("Location").getValue();
		EntityUtils.consume(resp2.getEntity());
//		System.out.println(EntityUtils.toString(resp2.getEntity()));
//     	System.out.println("---------------------------------------------------------------");
		
//		HttpGet get3=new HttpGet("http://uploaded.net/login");
//		get3.setHeader(HeaderUtils.getUserAgent());
//		get3.setHeader(HeaderUtils.getReferer("http://uploaded.net/login"));
//		HttpResponse resp4=httpclient.execute(get3);
//		EntityUtils.consume(resp4.getEntity());
//		System.out.println(EntityUtils.toString(resp4.getEntity()));
		
		HttpGet get2=new HttpGet("http://uploaded.net/me");
		get2.setHeader(HeaderUtils.getUserAgent());
		get2.setHeader(HeaderUtils.getReferer("http://uploaded.net/login/"));
		HttpResponse resp3=httpclient.execute(get2);
//		EntityUtils.consume(resp3.getEntity());
//		System.out.println(EntityUtils.toString(resp3.getEntity()));
		
		boolean valid = true;
		
		BufferedReader br=new BufferedReader(new InputStreamReader(resp3.getEntity().getContent(),"utf-8"));
		String line=null;
		
		boolean isPremium = false;
		String time = "";
		String email = "";
		while((line=br.readLine())!=null){
//			System.out.println(line);
			if(line.contains("Login")){
				valid=false;
			}
			
			if(line.contains("<em>Free</em>")){
				isPremium=false;
				time="NONE";
			}else{
				isPremium=true;
				time="";
			}
			
			if(line.contains("<th id=\"chMail\" title=\"")){
				Matcher m=EMAIL_PATTERN.matcher(line);
				m.find();
				email=m.group(1);
				System.out.println("EMAIL: "+email);
			}		
		}
		
		if(valid){
			return new UploadedAccount(user, email, password, isPremium, time);
		}else{
			return null;
		}
		
	}
	
	
//	public static void main(String[] args) throws Exception {
//		UploadedAccount ua = new UploadedChecker().login("","");
//		System.out.println(ua);
//	}

	public URL getImage() {
		return UploadedChecker.class.getResource("/accs/gui/img/uploadedto.png");
	}

	public String getName() {
		return "upl";
	}
}
